﻿//======================================================================
//
//        Copyright (C) 苏州逸仙软件    
//        All rights reserved
//
//        filename : userFunction.cs
//        description :
//
//        created by 周济 at 2010/12/28 18:04:20
//        mail:80406241@qq.com
//
//======================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace huaguwang.user
{
    public partial class userFunction : System.Web.UI.Page
    {
        private readonly D_T_Member_Exp _DalMember = new D_T_Member_Exp();
        protected void Page_Load(object sender, EventArgs e)
        {
            /*
             参数规则:
             
             * stype:
             *  0:获得登录资料
             *  1:判断用户名是否存在,p2密码，如果可以注册，则会把用户名和密码一起加密，使用空格分割
             *  3:用户登录 p1用户名p2密码 p3是否保存一个月
             *  4:检查加密的用户名和密码是否正确　
             
             其他的参数,顺序可以按照p1,p2....来进行,具体可以在相关的函数里面备注
             
             * 
             */

            Response.Clear();
            Response.ContentType = "text/plain";
            Response.Buffer = true;
            Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
            Response.Expires = 0;
            Response.CacheControl = "no-cache";
            Response.AddHeader("Pragma", "No-Cache");
            if (!IsPostBack)
            {
                var sType = Request.Params["stype"].Exp_IntTryParse();
                switch (sType)
                {
                    case 0: //获得登录信息
                        GetUser();
                        break;
                    case 1://检查用户名
                        IsUserName();
                        break;
                    case 3:
                        LoginName();
                        break;
                    case 4:
                        isNamePass();
                        break;
                    case 5:
                        RegUSer();
                        break;
                    default:
                        break;
                }
            }
            Response.End();
        }
        public void RegUSer()
        {
            var p = Request.Params["p1"].Exp_Trim();
            var p1 = Request.Params["p2"].Exp_Trim();
            var p2 = Request.Params["p3"].Exp_Trim();
            p = Server.UrlDecode(p);
            p = p.Exp_DecryptTry();
            var msg = "";
            if(0 >= p.Length)
            {
                msg = false.Exp_ExecuteSqlToJson("注册资料不完整，请重新填写");
            }
            else
            {
                var A = p.Split(' ');
                if(A.Length != 2)
                {
                    msg = false.Exp_ExecuteSqlToJson("注册资料不完整，请重新填写!");
                }
                else
                {
                    if(A[0].Length <= 0 || A[1].Length <= 0)
                    {
                        msg = false.Exp_ExecuteSqlToJson("用户名或密码为空，请重新填写");
                    }
                    else
                    {
                        var dal = new D_T_Member_Exp();
                        var model = new M_T_Member();
                        model.Email = p1;
                        model.Tel = p2;
                        model.Name = A[0];
                        model.PassWord = A[1].Exp_Encrypt(D_T_Member_Exp.MemBerKey);
                        model.Birthday = DateTime.Now;
                        model.FreezeDate = model.Birthday;
                        model.OpFreezeDate = model.Birthday;
                        model.IDCard = "";
                        var i = dal.Add(model);
                        if(i > 0)
                        {
                            msg = true.Exp_ExecuteSqlToJson("注册成功");
                        }
                        else
                        {
                            msg = false.Exp_ExecuteSqlToJson("注册失败");
                        }
                    }
                }
            }
            Response.Write(msg);
        }
        public void isNamePass()
        {
            var p = Request.Params["p1"].Exp_Trim();
            p = Server.UrlDecode(p);
            p = p.Exp_DecryptTry();
            var msg = "";
            if(0 >= p.Length)
            {
                msg = false.Exp_ExecuteSqlToJson("注册资料不完整，请重新填写");
            }
            else
            {
                var A = p.Split(' ');
                if(A.Length != 2)
                {
                    msg = false.Exp_ExecuteSqlToJson("注册资料不完整，请重新填写!");
                }
                else
                {
                    if(A[0].Length <= 0 || A[1].Length <= 0)
                    {
                        msg = false.Exp_ExecuteSqlToJson("用户名或密码为空，请重新填写");
                    }
                    else
                    {
                        msg = true.Exp_ExecuteSqlToJson(A[0] + " " + A[1]);
                    }
                }
            }
            Response.Write(msg);
        }
        public void LoginName()
        {
            var name = Request.Params["p1"].Exp_Trim();
            var pass = Request.Params["p2"].Exp_Trim();
            var isyz = Request.Params["p3"].Exp_Trim();
            var msg = "";
            if(name.Length > 30 || name.Length <= 0)
            {
                msg = false.Exp_ExecuteSqlToJson("用户名需要是1到30位的字符");
            }
            else
            {
                if(pass.Length > 30 || pass.Length <= 0)
                {
                    msg = false.Exp_ExecuteSqlToJson("密码需要是1到30位的字符");
                }
                else
                {
                    var model = _DalMember.MemBerLogin(name, pass.Exp_Encrypt(D_T_Member_Exp.MemBerKey));
                    //var modellog = new M_T_Member_Exp.M_T_MemberLogin(model);
                    //调用统一的转换函数,把用户实体,转换成登录实体
                    var modellog = model.Exp_ToModelLogin();
                    if(modellog == null)
                    {
                        msg = false.Exp_ExecuteSqlToJson("登录失败,请检查用户名和密码");
                    }
                    else
                    {
                        //如果需要保存cookie,则进行
                        if(isyz.ToLower().Equals("true"))
                        {
                            Response.Cookies.Remove(AdminPublic.M_SessionUser);
                            var c = new HttpCookie(AdminPublic.M_SessionUser);
                            //7天过期
                            c.Expires = DateTime.Now.AddDays(7);
                            c.Values.Add(AdminPublic.M_CookiesUserID, modellog.id.ToString());
                            c.Values.Add(AdminPublic.M_CookiesUserName, modellog.Name.ToString());
                            c.Values.Add(AdminPublic.M_CookiesUserPW, modellog.PassWord.ToString());
                            Response.Cookies.Add(c);
                        }
                        Session.Remove(AdminPublic.M_SessionUser);
                        Session.Add(AdminPublic.M_SessionUser, modellog);
                        //msg = true.Exp_ExecuteSqlToJson("登录成功");
                        msg = true.Exp_ExecuteSqlToJson(this.SessionURL_Get());
                    }
                }
            }
            Response.Write(msg);
        }
        public void IsUserName()
        {
            var name = Request.Params["p1"].Exp_Trim();
            var p1 = Request.Params["p2"].Exp_Trim();
            var msg = "";
            if(name.Length > 0)
            {
                var IsUser = _DalMember.IsURL(name, 0);
                if(IsUser)
                {
                    msg = false.Exp_ExecuteSqlToJson("该会员名已存在!");
                }
                else
                {
                    msg = true.Exp_ExecuteSqlToJson(Server.UrlEncode((name + " " + p1).Exp_Encrypt()));
                }
            }
            Response.Write(msg);
        }
        public void GetUser()
        {
            var model = Page.Exp_GetMember();
            var strJson = "";
            if(model == null)
            {
                strJson = false.Exp_ExecuteSqlToJson("");
            }
            else
            {
                strJson = true.Exp_ExecuteSqlToJson(model.Name + "  今天是:" + DateTime.Now.Exp_GetString());
            }
            Response.Write(strJson);
        }
    }
}
